<?php

class cntrlmanagephotos implements viewController {

	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "managePhotos.js"), "CSS" => array("facebox.css"));
	private $pageName = "";
	private $userObj;
	private $thisID;
	private $managedGroups;
	
	public function __construct(user &$userObj){

		if( $userObj->getAccessLevel() == -1 ){
			header("Location: home");
			exit;
		}
		
		$this->side = new template('control.manageImages');
		$this->area = new template('area.manageImages');

		$this->userObj = $userObj;
		
		parse_str( $_GET['act'] );

		$imageInfo = images::getImage($id);

		$this->thisID = $id;

		$this->managedGroups = $this->userObj->getManagedGroups(true);

		$this->renderPage($id, $imageInfo);
		$this->renderImageList();
	}

	private function renderPage($id, $imageInfo){
		$id = (int)$id;		
		
		$this->area->replaceAll(array(
			"groupName" => $imageInfo['name'],
			"groupDescription" => $imageInfo['description'],
			"imgID" => $imageInfo['imgID']
		));
		
		
		// Get a list of managed groups so that we can check them for images. Also get a list of user images so that we can display in select
	
		$toAdd = "";
		
		foreach($this->managedGroups as $myGroup){
			$toAdd .= "<option value='" . $myGroup['gID'] . "'>" . $myGroup['groupName'] . "</option>\n";
		}
		
		// Get all clones of this image.
		
		$clones = images::getClones($this->userObj, $id, images::LOCATION_GROUP);
				
		$select = "";
		foreach($clones as $clone){
			$select .= "<option value='" . $clone['group_gID'] . "'>" . $clone['name'] . "</option>\n";
		}		
		
		$this->area->replace('retrieveSelect', $select);
		$this->area->replace('donateSelect', $toAdd);
		
	}

	protected function renderImageList(){
		$myImages = images::getUserImages($this->userObj);
		$groupImages = array();
		
		foreach($this->managedGroups as $group){
			$groupImages = array_merge($groupImages, images::getGroupImages($this->userObj, $group['gID']));
		}
		
		$output = "";
		
		if(count($myImages) > 0){
			
			foreach($myImages as $image){
				if($this->thisID == $image['imgID']){
					$output .= "<option value='" . $image['imgID'] . "' selected='selected'>" . $image['name'] . "</option>\n";
				} else {
					$output .= "<option value='" . $image['imgID'] . "'>" . $image['name'] . "</option>\n";
				}
			}
		
		}

		if(count($groupImages) > 0){

			foreach($groupImages as $image){
				if(count($image) == 0) continue;
				if($this->thisID == $image['imgID']){
					$output .= "<option value='" . $image['imgID'] . "' selected='selected'>" . $image['name'] . "</option>\n";
				} else {
					$output .= "<option value='" . $image['imgID'] . "'>" . $image['name'] . "</option>\n";
				}
			}
		
		}
		
		$this->side->replace('groupList', $output);
		
		// Get a list of local albums we can use.
		$albums = images::getUserLists($this->userObj);
		
		if(count($albums) > 0){
			$output = "";
			foreach($albums as $album){
				if(count($album) == 0) continue;
				$output .= "<option value='" . $album['isID'] . "'>" . $album['name'] . "</option>\n";
			}
		
		}
		
		$this->area->replace('retrieveLocation', $output);
		
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return "";
	}

}

?>